/*
 * @Descripttion: 
 * @version: 
 * @Author: wangzhilei
 * @Date: 2021-10-07 11:17:53
 * @LastEditors: wangzhilei
 * @LastEditTime: 2021-10-07 11:53:35
 */
#include<signal.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>

void handler(int sig){
    printf("get a sig,num is:%d\n",sig);
}
void print_sig(sigset_t *p)
{
    int i=1;
    for(;i<32;++i){
        if(sigismember(p,i)){
            printf("1");
        }else{
            printf("0");
        }
    }
    printf("\n");
}
int main(){
    signal(2,handler);

    sigset_t s,p,o;
    sigemptyset(&s);
    sigemptyset(&o);
    sigaddset(&s,SIGINT);

    sigprocmask(SIG_SETMASK,&s,&o);//信号集s中的SIGINT设置为屏蔽

    int count=0;

    while (1)
    {
        sigemptyset(&p);
        sigpending(&p);//当前进程已收到的未决信号(屏蔽的信号),解决屏蔽时会执行一次P中的未决信号
        print_sig(&p);
        sleep(1);
        if (count++==10)
        {
            sigprocmask(SIG_SETMASK,&o,NULL);//将之前的屏蔽信号集设置回去,并执行p未决集中的信号
            printf("recover block\n");
            sleep(3);
            count=0;
        }
        
    }
    
}